<template>
  <div class="tags">
    <el-tag
      v-for="(tag, index) in tabList"
      :key="tag.name"
      :effect="$route.name === tag.name ? 'dark' : 'plain'"
      :closable="tag.name !== 'home'"
      @close="handleClose(tag, index)"
      @click="changeMenu(tag)"
      size="small"
    >
      {{ tag.label }}
    </el-tag>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  computed: {
    ...mapState("tab", ["tabList"]),
  },
  methods: {
    ...mapMutations("tab", ["closeTag"]),
    changeMenu(tag) {
      this.$router.push({ name: tag.name });
    },
    handleClose(tag, index) {
      const length = this.tabList.length - 1;
      this.closeTag(tag);
      if (this.$route.name !== tag.name) {
        return;
      }
      if (length === index) {
        this.$router.push({ name: this.tabList[index - 1].name });
      } else {
        this.$router.push({ name: this.tabList[index].name });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.tags {
  padding: 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
